package mirkofrancuski.battleship;

import java.util.List;

import mirkofrancuski.battleship.db.DatabaseHelper;
import mirkofrancuski.battleship.db.HighScoreInfo;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class HighScoreActivity extends Activity {

	private TableLayout m_tableLayout = null;
	private Typeface m_font_title;
	public static DatabaseHelper m_dbHelper = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.activity_high_scores);

		m_dbHelper = new DatabaseHelper(this);

		m_tableLayout = (TableLayout) findViewById(R.id.table_high_scores);

		m_font_title = Typeface.createFromAsset(getAssets(), "Army.ttf");
	}

	@Override
	protected void onResume() {
		super.onResume();
		ClearList();
		LoadList();
	}

	public void CreateHeader() {

		TableRow.LayoutParams params = new TableRow.LayoutParams();
		params.span = 12;

		TableRow headerRow = new TableRow(this);

		TextView rank = new TextView(this);
		rank.setText("Rank");

		TextView player = new TextView(this);
		player.setText("Player");

		TextView adversary = new TextView(this);
		adversary.setText("Adversary");

		TextView moves = new TextView(this);
		moves.setText("Moves");

		rank.setTypeface(m_font_title);
		player.setTypeface(m_font_title);
		adversary.setTypeface(m_font_title);
		moves.setTypeface(m_font_title);

		rank.setTextColor(Color.WHITE);
		player.setTextColor(Color.WHITE);
		adversary.setTextColor(Color.WHITE);
		moves.setTextColor(Color.WHITE);

		headerRow.addView(rank);
		headerRow.addView(player);
		headerRow.addView(adversary);
		headerRow.addView(moves);

		m_tableLayout.addView(headerRow, params);
	}

	public void ClearList() {
		m_tableLayout.removeAllViews();
		CreateHeader();
	}

	public void LoadList() {
		List<HighScoreInfo> allHighScores = m_dbHelper.getAllHighScores();
		for (HighScoreInfo highScoreInfo : allHighScores) {
			TableRow highScoreRow = new TableRow(this);

			TextView rank = new TextView(this);
			rank.setText(String.valueOf(highScoreInfo.getRank()) + ".");

			TextView player = new TextView(this);
			player.setText(highScoreInfo.getPlayerName());

			TextView adversary = new TextView(this);
			adversary.setText(highScoreInfo.getAdversaryName());

			TextView moves = new TextView(this);
			moves.setText(String.valueOf(highScoreInfo.getNumberOfMoves()));

			rank.setTypeface(null,Typeface.BOLD);
			player.setTypeface(null,Typeface.BOLD);
			adversary.setTypeface(null,Typeface.BOLD);
			moves.setTypeface(null,Typeface.BOLD);

			rank.setTextColor(Color.WHITE);
			player.setTextColor(Color.WHITE);
			adversary.setTextColor(Color.WHITE);
			moves.setTextColor(Color.WHITE);

			highScoreRow.addView(rank);
			highScoreRow.addView(player);
			highScoreRow.addView(adversary);
			highScoreRow.addView(moves);

			m_tableLayout.addView(highScoreRow);
		}
	}
}
